const projMod = require('../model/projMod.js')
const SnowflakeID = require('../utils/SnowflakeID')

const projServ = {
  deplistCountServ: async () => {
    const res = await projMod.deplistCountMod()
    let data = []
    for (let k in res[0]) {
      data.push(res[0][k])
    }
    return data
  },
  collectStatusServ: async (params) => {
    let res = await projMod.collectStatusMod(params)
    return {
      cStatus: res
    }
  },
  burryPointServ: async (params) => {
    let res = await projMod.burryPointMod(params)
    res = res[0]
    return res
  },
  collctProjServ: async (params) => {
    await projMod.collctProjMod(params)
  },
  deletColletServ: async (params) => {
    await projMod.deletColletMod(params)
  },
  collectListServ: async (userId) => {
    const res = await projMod.collectListMod(userId)
    return res
  },
  myProjlistServ: async (userId) => {
    const res = await projMod.myProjlistMod(userId)
    return res
  },
  projDetailServ: async (projId) => {
    let data = await projMod.projDetailMod(projId)
    data = data[0]
    return data
  },
  getprojListServ: async (params) => {
    const data = await projMod.getprojListMod(params)

    let res = data.map(i => {
      return {
        ...i,
        tagslist: [i.depName, i.grade ? `${i.grade}级` : '年级不限']
      }
    })
    res.forEach(i => {
      delete i.depName
      delete i.grade
    });

    // 随机数组
    res.sort(() => {
      return (0.5 - Math.random());
    })
    return res
  },
  addprojServ: async (params) => {
    let pid = new SnowflakeID({
      mid: +new Date()
    }).generate()
    params.pid = pid
    await projMod.addprojtMod(params)
  },
  deleteprojServ: async (projId) => {
    await projMod.deleteprojMod(projId)
  },
}

module.exports = projServ